#include <iostream>
#include <gio/gio.h>
#include "ExampleGdbusGenerated.h"

int main() {
#if 1
    GDBusConnection * connection;
    GError *error = nullptr;

    std::string serverUnixPath = "unix:abstract=/org/openkylin/aisdk/demo/address";
    connection = g_dbus_connection_new_for_address_sync(
            serverUnixPath.c_str(), G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
            nullptr, /* GDBusAuthObserver */
            nullptr, /* GCancellable */
            &error);
    if (connection == nullptr) {
        g_printerr("Error connecting to D-Bus address %s: %s\n",
                   serverUnixPath.c_str(), error->message);
        g_error_free(error);
        return -1;
    }

    g_print ("Connected. Negotiated capabilities: unix-fd-passing=%d\n",
             g_dbus_connection_get_capabilities(connection) & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);

    std::string objectPath = "/org/openkylin/aisdk/demo/object";
    AisdkDemo *proxy = aisdk_demo_proxy_new_sync(
            connection, G_DBUS_PROXY_FLAGS_NONE, nullptr, objectPath.c_str(),
            nullptr, &error);
    if (proxy == nullptr) {
        g_printerr("Error creating speech processor proxy %s: %s\n",
                   objectPath.c_str(), error->message);
        g_error_free(error);
        return -1;
    }

    g_print("connected server.\n");
#endif
    int output = 0;
    gboolean success;
    int param1 = 10, param2 = 20;
    GVariant *pVariant = g_variant_new("(ii)", param1, param2);
    GVariant *input = g_variant_new("v", pVariant);



//    success = aisdk_demo_call_method1_sync(proxy, input, &output, nullptr, &error);
//    if (!success) {
//        if (error) {
//            g_printerr("Call method1 Error: %s\n", error->message);
//            g_error_free(error);
//            return -1;
//        }
//    }
//
//    g_print("Call method1 result: %d\n", output);

    gchar *message = nullptr;
    gint errorNo;
    GVariant *tmp = g_variant_new("(s)", "hello");
    success = aisdk_demo_call_method2_sync(proxy, tmp, &errorNo, &message, nullptr, &error);
    if (!success) {
        if (error) {
            g_printerr("Call method4 Error: %s\n", error->message);
            g_error_free(error);
            return -1;
        }
    }

    g_print("Call method4 result: %d %s\n", errorNo, message);

    return 0;
}